package org.oimwrapper.connectors.internal;

import com.thortech.xl.dataaccess.*;
import java.sql.*;
import java.util.*;

/**
 * JDBC connection interface into OIM.
 * 
 * THIS IS AN INTERNAL CLASS AND SHOULD NOT BE USED DIRECTLY.
 */
final class OIMDBConnection implements Connection {
	
	/**
	 * The OIM database being used.
	 */
	private tcDataProvider database;
	
	/**
	 * <code>true</code> if the database should be committed automatically, or <code>false</code>
	 * if it should be committed manually;
	 */
	private boolean autoCommit;

	/**
	 * Default constructor.
	 * 
	 * @param Database The OIM database being used.
	 */
	protected OIMDBConnection(tcDataProvider Database) {
		database = Database;
		autoCommit = false;
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public void clearWarnings() throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * Closes the database connection. This does nothing, as the OIM database connection
	 * can never be fully closed.
	 * 
	 * @exception SQLException
	 */
	public void close() throws SQLException { ; }

	/**
	 * Commits any changes to the database.
	 * 
	 * @exception SQLException
	 */
	public void commit() throws SQLException {
		try {
			database.commitTransaction();
		} catch (Exception e) {
			throw new SQLException(e.getMessage());
		}
	}

	/**
	 * Indicates of auto commit is enabled or not.
	 * 
	 * @return <code>true</code> if transactions are commited automatically, or
	 *         <code>false</code> if transactions need to be commited manually.
	 * @exception SQLException
	 */
	public boolean getAutoCommit() throws SQLException {
		return autoCommit;
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public String getCatalog() throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public int getHoldability() throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public DatabaseMetaData getMetaData() throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * Indicates the current transaction isolation level.
	 * 
	 * @return The current transaction isolation level.
	 * @exception SQLException
	 */
	public int getTransactionIsolation() throws SQLException {
		return Connection.TRANSACTION_SERIALIZABLE;
	}

	/**
	 * Gets the custom type Map of the connection.  OIM does not use any custom
	 * types, so this will always be empty.
	 * 
	 * @return The custom type Map for the connection.
	 * @exception SQLException
	 */
	public Map getTypeMap() throws SQLException {
		return new HashMap();
	}

	/**
	 * Gets the warnings from the database connection.
	 * 
	 * @return OIM does not generate specific database warnings, so this will always
	 *         be null.
	 * @exception SQLException
	 */
	public SQLWarning getWarnings() throws SQLException {
		return null;
	}

	/**
	 * Indicates if the connection is closed or not.
	 * 
	 * @return Always <code>false</code>, as the OIM connection cannot be closed.
	 * @exception SQLException
	 */
	public boolean isClosed() throws SQLException {
		return false;
	}

	/**
	 * Indicates if the connection is read only or not.
	 * 
	 * @return Always <code>false</code>, as the OIM connection is always writeable.
	 * @exception SQLException
	 */
	public boolean isReadOnly() throws SQLException {
		return false;
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public String nativeSQL(String Query) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public void releaseSavepoint(Savepoint Point) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * Rolls back any in flight transaction not yet commited to the database.
	 * 
	 * @exception SQLException
	 */
	public void rollback() throws SQLException {
		try {
			database.rollbackTransaction();
		} catch (Exception e) {
			throw new SQLException(e.getMessage());
		}
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public void rollback(Savepoint Point) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * Sets the database to autocommit or not.
	 * 
	 * @param Flag <code>true</code> if data should be committed automatically, or 
	 *             <code>false</code> if data should be committed manually.
	 * @exception SQLException
	 */
	public void setAutoCommit(boolean Flag) throws SQLException {
		try {
			database.setAutoCommit(Flag);
			autoCommit = Flag;
		} catch (Exception e) {
			throw new SQLException(e.getMessage());
		}
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public void setCatalog(String Catalog) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public void setHoldability(int Holdability) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public void setReadOnly(boolean Flag) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public Savepoint setSavepoint() throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public Savepoint setSavepoint(String Name) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public void setTransactionIsolation(int Isolation) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public void setTypeMap(Map TypeMap) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}
	
	/**
	 * Creates an new statement against the OIM database.
	 * 
	 * @return A new statement against the OIM database.
	 * @exception SQLException
	 */
	public Statement createStatement() throws SQLException {
		return new OIMDBStatement(this, database, null);
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public Statement createStatement(int ResultSetType, int ResultSetConcurrency) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public Statement createStatement(int ResultSetType, int ResultSetConcurrency, int ResultSetHoldability) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}
	
	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public CallableStatement prepareCall(String Query) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public CallableStatement prepareCall(String Query, int ResultSetType, int ResultSetConcurrency) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public CallableStatement prepareCall(String Query, int ResultSetType, int ResultSetConcurrency, int ResultSetHoldability) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * Creates an new prepared statement against the OIM database.
	 * 
	 * @return A new prepared statement against the OIM database.
	 * @exception SQLException
	 */
	public PreparedStatement prepareStatement(String Query) throws SQLException {
		return new OIMDBStatement(this, database, Query);
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public PreparedStatement prepareStatement(String Query, int AutoGeneratedKeys) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public PreparedStatement prepareStatement(String Query, int ResultSetType, int ResultSetConcurrency) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public PreparedStatement prepareStatement(String Query, int ResultSetType, int ResultSetConcurrency, int ResultSetHoldability) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public PreparedStatement prepareStatement(String Query, int[] ColumnIndexes) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

	/**
	 * This method is not supported.
	 * 
	 * @exception SQLException
	 */
	public PreparedStatement prepareStatement(String Query, String[] ColumnNames) throws SQLException {
		throw new UnsupportedOperationException("Not supported.");
	}

    @Override
    public Clob createClob() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Blob createBlob() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public NClob createNClob() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public SQLXML createSQLXML() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean isValid(int timeout) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setClientInfo(String name, String value) throws SQLClientInfoException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setClientInfo(Properties properties) throws SQLClientInfoException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getClientInfo(String name) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Properties getClientInfo() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}